<template>
  <div class="h100 w100">
    <div class="settledEnt-top" @click="gohome()">
      <img src="~@/assets/reception/logo.png" />
    </div>
    <div class="container a-df">
      <div class="latest-entry">
        <span class="latest-title">热门新闻</span>
        <div class="latest-entry-items">
          <div class="latest-entry-item" :class="{ 'isshow-news': item.isshow }"  v-for="(item,index) in latestEntry" :key="index" @click="checknews(item.ID)">
            <img :src="upload + item.Imgs" alt="">
            <span class="item-title">{{ displayContent(item.NAME,10) }}</span>
            <span class="item-content">{{ copyText(item.MS) }}</span>
            <span class="item-time">{{ item.AddTime }}</span>
          </div>
        </div>
      </div>
      <div class="latest-detail">
        <div class="detail-title">
          <span>{{ newsDetail.NAME }}</span>
          <span class="detail-time">{{ newsDetail.AddTime }}</span>
        </div>
        <div class="detail-service-details">
          <div class="service-content">
            <!-- {{ newsDetail.MS }} -->
            <!-- <article style="background-color: white;" v-html="newsDetail.MS"></article> -->
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getAppGetNewsListApi, getAppGetNewsByIDApi } from '@/api/resService'

export default {
  name: 'newsdetail',
  data () {
    return {
      upload:process.env.VUE_APP_API_BASE_URL,
      latestEntry:[],
      newsDetail:{}
    }
  },
  methods: {
    displayContent(text , num) {
      if ( text <= num) {
        return text;
      }else {
        return text.slice(0, num) + '...';
      }
    },
    copyText(val) { // 要判断一下,如果是空就返回空字符串,不然会报错
      if (val != null && val != '') {
        var reg = /[\u4e00-\u9fa5]/g
        var names = val.match(reg)
        val = names.join('')
        return val.substr(0, 100);//获取富文本文字内容的前50个字符
      } else return ''
    },
    checknews(id){
      for (const item of this.latestEntry) {
        if (item.ID == id) {
          item.isshow=true
        }else{
          item.isshow=false
        }
      }
      getAppGetNewsByIDApi({
          ID:id
        }).then((res)=>{
          console.log('res:', res);
          this.newsDetail = res.data[0]
        }).catch((err)=>{
          console.log('err:', err);
        })
    },
    getAppGetNewsList(){
      getAppGetNewsListApi(
        {
          pageSize:10,
          pageIndex:1,
          Fid:2,
        }
      ).then((res) => {
        console.log('res',res);
        this.latestEntry = res.data
        for (const item of this.latestEntry) {
          this.$set(item, 'isshow', false);
        }
        this.latestEntry[0].isshow=true
      })
      .catch((err) => {
      })
      .finally(() => {
      })
    },
    gohome() {
      this.$router.push({ path: '/reception/findServices/index' });
    },
  },
  mounted() {
    this.getAppGetNewsList()
  },
  watch:{
    '$route.query': {
      immediate: true, // 如果需要在组件创建时立即触发，设置为true
      handler(newVal, oldVal) {
        // 当路由参数变化时，这里会被调用
        // newVal 是新的参数，oldVal 是旧的参数
        console.log('Route parameter changed:', newVal,this.$route.query);
        // 你可以在这里根据新的参数执行相应的操作
        getAppGetNewsByIDApi({
          ID:this.$route.query.id
        }).then((res)=>{
          console.log('res:', res);
          this.newsDetail = res.data[0]
        }).catch((err)=>{
          console.log('err:', err);
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
.isshow-news{
  box-shadow: 2px 4px 8px #2f71f8;
}
.settledEnt-top{
  height: 60px;
  background: #fff;
  padding: 10px 100px ;
  cursor: pointer;
  img{
    height: 40px;
    display: block;
  }
}
.latest-entry{
  width: 300px;
  background: #ededed;
  border-radius: 20px;
  height: 860px;
  overflow-y: auto;
  .latest-title{
    display: block;
    background: #486dee;
    color: #fff;
    text-align: center;
    line-height: 50px;
    font-size: 24px;
    width: 100%;
    height: 50px;
    margin-bottom: 20px;
  }
  .latest-entry-items{
    display: flex;
    flex-direction:column;
    .latest-entry-item{
      cursor: pointer;
      background: #fff;
      border-radius:10px ;
      overflow: hidden;
      // height: 300px;
      margin: 20px;
      padding-bottom:10px;
      img{
        width: 100%;
        height: 150px;
      }
      .item-title{
        display: block;
        font-size: 18px;
        width: 260px;
        margin: 10px 20px;
      }
      .item-content{
        display: block;
        // font-size: 18px;
        // width: 260px;
        margin: 10px 20px;
      }
      .item-time{
        display: block;
        color: #cfcece;
        margin: 10px 20px;
      }
    }
  }
}
.latest-detail{
  margin: 20px;
  flex: 1;
  .detail-title{
    font-size: 24px;
    margin: 20px 0;
    border-bottom: solid #dddbdb 2px;
    display: flex;
    .detail-time{
      text-align: end;
      flex: 1;
      font-size: 16px;
      line-height: 35px;
    }
  }
  .detail-introduce{
    span{
      display: inline-block;
      margin-right: 10px;
    }
  }
  .detail-price{
    margin: 20px 0;
    span{
      color: #d47676;
    }
  }
  .detail-qualification{
    text-align: center;
    .qualification-title{
      margin: 10px 0;
      color: #cfcece;
    }
    .qualification-item{
      img{
        margin: 0 20px;
      }
    }
  }
}
.detail-service-details{
  // width: 100%;
  .service-title{
    display: flex;
    justify-content: center;
    margin: 20px auto;
    font-size: 24px;
    span{
      background: #2f71f8;
      display: inline-block;
      // margin: 0 5px;
      justify-content: center;
      align-items: center;
      border-radius: 20px;
      margin: auto 5px;
    }
    p{
      display: flex;
      margin: 0 20px;
    }
    .span1{
      width: 5px;
      height: 5px;
    }
    .span2{
      width: 10px;
      height: 10px;
    }
    .span3{
      width: 15px;
      height: 15px;
    }
  }
  .service-content{
    width: 100%;
    font-size: 18px;
    text-indent: 36px;
  }
}
</style>
